Is the name
of a method inside or outside
the glass box?
Outside—so it can be "seen" and used by other methods.
Within the body of a method, a parameter can be used just like any variable. It can be used in arithmetic expressions, in assignment statements, and so on.
However, changes made to the parameter do not have any effect outside the method body. A parameter is a "local copy" of whatever value the caller passed into the method. Any changes made to it affect only this local copy. For example:
class CheckingAccount { . . . . private int balance; void processCheck( int amount ) { int charge; if ( balance < 100000 ) charge = 15; else charge = 0; balance = balance - amount - charge ; // change the local copy in "amount" amount = 0 ; } } class CheckingTester { CheckingAccount act; public static void main ( String[] args ) { int check = 5000; act = new CheckingAccount( "123-345-99", "Wanda Fish", 100000 ); // prints "5000" System.out.println( "check:" + check ); // call processCheck with the value 5000 act.processCheck( check ); // prints "5000" --- "check" was not changed System.out.println( "check:" + check ); } }
The formal parameter amount
is
the name used by processCheck()
for the value 5000
that it has been given by the caller.
The method can change the value held in amount
,
but this has no effect on the caller's variables.
This subject will be further discussed in a future chapter. For now, regard a parameter as a "one-way message" that the caller uses to send values to the method.